<template>
    <div class="app-container calendar-list-container">
        <div class="filter-container">
            <span>使用公司:</span>
            <el-select  style="width: 120px" class="filter-item"  placeholder="全部">
                <el-option label="全部" value=1></el-option>
                <el-option label="深圳卷皮网络科技有限公司" value=2></el-option>
            </el-select>

            <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="标题" v-model="listQuery.title">
            </el-input>

            <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
            <router-link :to="{ path:'list/add'}">
                <el-button class="filter-item"  type="primary" icon="plus">增加</el-button>
            </router-link>
            <el-button class="filter-item" type="primary" icon="document" @click="handleDownload">导入/导出</el-button>
        </div>

        <el-table :key='tableKey' :data="list" v-loading.body="listLoading" stripe border fit highlight-current-row style="width: 100%">

            <el-table-column align="center" label="序号" width="65">
                <template scope="scope">
                    <span>{{scope.row.Id}}</span>
                </template>
            </el-table-column>

            <el-table-column label="状态" >
                <template scope="scope">
                    <span>{{scope.row.AsStatus}}</span>
                </template>
            </el-table-column>

            <el-table-column label="资产编号" width="120px" align="center" >
                <template scope="scope">
                    <span>{{scope.row.AsCode}}</span>
                </template>
            </el-table-column>

            <el-table-column label="资产名称"  min-width="200px" >
                <template scope="scope">
                    <span>{{scope.row.AsName}}</span>
                </template>
            </el-table-column>

            <el-table-column label="资产类别" width="120px" align="center" >
                <template scope="scope">
                    <span>{{scope.row.TypeId}}</span>
                </template>
            </el-table-column>

            <el-table-column label="SN号"  width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsSn}}</span>
                </template>
            </el-table-column>

            <el-table-column label="品牌" width="120px" align="center" >
                <template scope="scope">
                    <span>{{scope.row.AsBrand}}</span>
                </template>
            </el-table-column>

            <el-table-column label="规格型号" width="120px" align="center" >
                <template scope="scope">
                    <span>{{scope.row.AsSpec}}</span>
                </template>
            </el-table-column>

            <el-table-column label="配置" width="120px" align="center" >
                <template scope="scope">
                    <span>{{scope.row.AsAllocation}}</span>
                </template>
            </el-table-column>

            <el-table-column label="购置时间" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsBuyTime | parseTime('{y}-{m}-{d}')}}</span>
                </template>
            </el-table-column>

            <el-table-column label="计量单位" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsUnit}}</span>
                </template>
            </el-table-column>

            <el-table-column label="采购价格"  width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsAmount}}</span>
                </template>
            </el-table-column>

            <el-table-column label="原值"  width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsOriginal}}</span>
                </template>
            </el-table-column>

            <el-table-column label="使用公司" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsUsedComp}}</span>
                </template>
            </el-table-column>

            <el-table-column label="使用部门" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsUsedDep}}</span>
                </template>
            </el-table-column>

            <el-table-column label="使用人" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsUsedPerson}}</span>
                </template>
            </el-table-column>

            <el-table-column label="区域" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsArea}}</span>
                </template>
            </el-table-column>

            <el-table-column label="存放地点" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsLocation}}</span>
                </template>
            </el-table-column>

            <el-table-column label="使用期限(月)" >
                <template scope="scope">
                    <span>{{scope.row.AsTimeLimit}}</span>
                </template>
            </el-table-column>

            <el-table-column label="所属公司" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsBelongComp}}</span>
                </template>
            </el-table-column>

            <el-table-column label="供应商" width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsSupplier}}</span>
                </template>
            </el-table-column>

            <el-table-column label="来源"  width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsFrom}}</span>
                </template>
            </el-table-column>

            <el-table-column label="管理员"  width="120px" align="center">
                <template scope="scope">
                    <span>{{scope.row.AsManager}}</span>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
                           :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form class="small-space" :model="temp" label-position="left" label-width="70px" style='width: 400px; margin-left:50px;'>
                <el-form-item label="类型">
                    <el-select class="filter-item" v-model="temp.type" placeholder="请选择">
                        <el-option v-for="item in  calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="状态">
                    <el-select class="filter-item" v-model="temp.status" placeholder="请选择">
                        <el-option v-for="item in  statusOptions" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="时间">
                    <el-date-picker v-model="temp.timestamp" type="datetime" placeholder="选择日期时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="标题">
                    <el-input v-model="temp.title"></el-input>
                </el-form-item>

                <el-form-item label="重要性">
                    <el-rate style="margin-top:8px;" v-model="temp.importance" :colors="['#99A9BF', '#F7BA2A', '#FF9900']"></el-rate>
                </el-form-item>

                <el-form-item label="点评">
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入内容" v-model="temp.remark">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button v-if="dialogStatus=='create'" type="primary" @click="create">确 定</el-button>
                <el-button v-else type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="阅读数统计" :visible.sync="dialogPvVisible" size="small">
            <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
                <el-table-column prop="key" label="渠道"> </el-table-column>
                <el-table-column prop="pv" label="pv"> </el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">确 定</el-button>
      </span>
        </el-dialog>

    </div>
</template>

<script>
    import { fetchList } from 'api/am_list';
    import { parseTime } from 'utils';

    const calendarTypeOptions = [
        { key: 'CN', display_name: '中国' },
        { key: 'US', display_name: '美国' },
        { key: 'JP', display_name: '日本' },
        { key: 'EU', display_name: '欧元区' }
    ];

    // arr to obj
    const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
                acc[cur.key] = cur.display_name;
    return acc
    }, {});

    export default {
        name: 'table_demo',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    limit: 20,
                    importance: undefined,
                    title: undefined,
                    type: undefined,
                    sort: '+id'
                },
                temp: {
                    id: undefined,
                    importance: 0,
                    remark: '',
                    timestamp: 0,
                    title: '',
                    type: '',
                    status: 'published'
                },
                importanceOptions: [1, 2, 3],
                calendarTypeOptions,
                sortOptions: [{ label: '按ID升序列', key: '+id' }, { label: '按ID降序', key: '-id' }],
                statusOptions: ['published', 'draft', 'deleted'],
                dialogFormVisible: false,
                dialogStatus: '',
                textMap: {
                    update: '编辑',
                    create: '创建'
                },
                dialogPvVisible: false,
                pvData: [],
                showAuditor: false,
                tableKey: 0
            }
        },
        created() {
            this.getList();
        },
        filters: {
            statusFilter(status) {
                const statusMap = {
                    published: 'success',
                    draft: 'gray',
                    deleted: 'danger'
                };
                return statusMap[status]
            },
            typeFilter(type) {
                return calendarTypeKeyValue[type]
            }
        },
        methods: {
            getList() {
                this.listLoading = true;
                fetchList(this.listQuery).then(response => {
                  this.list = response.data;
                  this.total = response.data.total;
                  this.listLoading = false;
            })
            },
            handleFilter() {
                this.getList();
            },
            handleSizeChange(val) {
                this.listQuery.limit = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.listQuery.page = val;
                this.getList();
            },
            timeFilter(time) {
                if (!time[0]) {
                    this.listQuery.start = undefined;
                    this.listQuery.end = undefined;
                    return;
                }
                this.listQuery.start = parseInt(+time[0] / 1000);
                this.listQuery.end = parseInt((+time[1] + 3600 * 1000 * 24) / 1000);
            },
            handleModifyStatus(row, status) {
                this.$message({
                    message: '操作成功',
                    type: 'success'
                });
                row.status = status;
            },
            handleCreate() {
                this.resetTemp();
                this.dialogStatus = 'create';
                this.dialogFormVisible = true;
            },
            handleUpdate(row) {
                this.temp = Object.assign({}, row);
                this.dialogStatus = 'update';
                this.dialogFormVisible = true;
            },
            handleDelete(row) {
                this.$notify({
                    title: '成功',
                    message: '删除成功',
                    type: 'success',
                    duration: 2000
                });
                const index = this.list.indexOf(row);
                this.list.splice(index, 1);
            },
            create() {
                this.temp.id = parseInt(Math.random() * 100) + 1024;
                this.temp.timestamp = +new Date();
                this.temp.author = '原创作者';
                this.list.unshift(this.temp);
                this.dialogFormVisible = false;
                this.$notify({
                    title: '成功',
                    message: '创建成功',
                    type: 'success',
                    duration: 2000
                });
            },
            update() {
                this.temp.timestamp = +this.temp.timestamp;
                for (const v of this.list) {
                    if (v.id === this.temp.id) {
                        const index = this.list.indexOf(v);
                        this.list.splice(index, 1, this.temp);
                        break;
                    }
                }
                this.dialogFormVisible = false;
                this.$notify({
                    title: '成功',
                    message: '更新成功',
                    type: 'success',
                    duration: 2000
                });
            },
            resetTemp() {
                this.temp = {
                    id: undefined,
                    importance: 0,
                    remark: '',
                    timestamp: 0,
                    title: '',
                    status: 'published',
                    type: ''
                };
            },
            handleFetchPv(pv) {
                fetchPv(pv).then(response => {
                    this.pvData = response.data.pvData;
                this.dialogPvVisible = true;
            })
            },
            handleDownload() {
                require.ensure([], () => {
                    const { export_json_to_excel } = require('vendor/Export2Excel');
                const tHeader = ['时间', '地区', '类型', '标题', '重要性'];
                const filterVal = ['timestamp', 'province', 'type', 'title', 'importance'];
                const data = this.formatJson(filterVal, this.list);
                export_json_to_excel(tHeader, data, 'table数据');
            })
            },
            formatJson(filterVal, jsonData) {
                return jsonData.map(v => filterVal.map(j => {
                            if (j === 'timestamp') {
                    return parseTime(v[j])
                } else {
                    return v[j]
                }
            }))
            }
        }
    }
</script>
